<?php

namespace App\Http\Controllers;

use App\Exceptions\GeneralException;
use App\Models\Banner;
use App\Models\System\SysLog;
use Illuminate\Http\Request;
use App\Repositories\BannerRepository;
use App\Http\Requests\Logic\Banner\BannerUpdate;
use App\Http\Requests\Logic\Banner\BannerCreate;
use App\Http\Requests\Logic\Banner\BannerUpload;
use App\Http\Requests\Logic\Banner\IndexRequest;
use App\Http\Requests\Logic\Banner\BannerDestroy;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;

/**
 * Banner 模块控制器
 *
 * Date: 12/04/2018
 * @author Sam
 * @package App\Http\Controllers
 */
class BannerController extends Controller
{
    protected $bannerRepository;

    /**
     * BannerController constructor.
     * @param BannerRepository $bannerRepository
     */
    public function __construct(
        BannerRepository $bannerRepository
    )
    {
        $this->bannerRepository = $bannerRepository;
    }

    /**
     * 根据公司Id 获取banner资源
     *
     * Date: 2018/4/10
     * @author Sam
     * @param IndexRequest $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function index(IndexRequest $request)
    {
        $company_id = $request->user()->company_id;
        $banner = $this->bannerRepository->getBanners($company_id);
        return success($banner);
    }


    /**
     * 新建banner资源
     *
     * Date: 2018/4/10
     * @author Sam
     * @param BannerCreate $request
     * @return \Illuminate\Http\JsonResponse
     * @throws \App\Exceptions\GeneralException
     */
    public function store(BannerCreate $request)
    {
        $attributes = $this->extractInputFromRules($request, $request->rules());
        $attributes['company_id'] = $request->user()->company_id;
        $banner = $this->bannerRepository->create($attributes);
        SysLog::log(Auth::user(),'轮播模块',"添加轮播");
        return stored($banner);
    }

    /**
     * 根据id获取banner资源
     *
     * Date: 2018/4/10
     * @author Sam
     * @param Request $request
     * @param Banner $banner
     * @return \Illuminate\Http\JsonResponse
     */
    public function show(Request $request, Banner $banner)
    {
        return success($banner);
    }


    /**
     * 更新banner资源
     *
     * Date: 2018/4/10
     * @author Sam
     * @param BannerUpdate $request
     * @param Banner $banner
     * @return \Illuminate\Http\JsonResponse
     */
    public function update(BannerUpdate $request, Banner $banner)
    {
        $attributes = $this->extractInputFromRules($request, $request->rules());
        DB::beginTransaction();
        try {
            $banner->update($attributes);
            SysLog::log(Auth::user(),'轮播模块',"更新轮播信息");
            DB::commit();
            return updated($banner);
        } catch (Exception $exception) {
            DB::rollBack();
           throw new GeneralException('未知错误！');
        }
    }

    /**
     * 销毁banner资源
     *
     * Date: 2018/4/10
     * @author Sam
     * @param BannerDestroy $request
     * @param Banner $banner
     * @return \Illuminate\Http\JsonResponse
     * @throws \App\Exceptions\GeneralException
     */
    public function destroy(BannerDestroy $request, Banner $banner)
    {
        $this->bannerRepository->destroy($banner);
        SysLog::log(Auth::user(),'轮播模块',"删除轮播");
        return deleted();
    }

    /**
     * 上传banner
     *
     * Date: 2018/4/10
     * @author Sam
     * @param BannerUpload $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function upload(BannerUpload $request)
    {
        $path = $request->file('banner')->store('banner');
        return success($path);
    }
}
